package com.mobinters.cafeman.adapter;

import java.util.List;

import com.mobinters.cafeman.R;
import com.mobinters.cafeman.object.AreaItem;
import com.mobinters.cafeman.object.Table;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.TextView;

public class TableAdapter extends BaseAdapter{
	List<Table> mData;
	Context mContext;
	LayoutInflater mInflater;
	
	public TableAdapter(Context context,List<Table> data){
		mContext = context;
		mData = data;
		mInflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		if(convertView == null){
			convertView = mInflater.inflate(R.layout.dlg_table_item, parent, false);
		}
		final Table item = mData.get(position);
		TextView tvName = (TextView) convertView.findViewById(R.id.txt_table_item_name);
		CheckBox cb = (CheckBox) convertView.findViewById(R.id.cb_table_item);
		FrameLayout layout = (FrameLayout) convertView.findViewById(R.id.layout_table_item);
		
		if(item.End) {
				tvName.setVisibility(View.GONE);
				cb.setVisibility(View.GONE);
				layout.setBackgroundColor(Color.WHITE);
				layout.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						//item.End = false;
						mData.get(position).End = false;//??NOT EFFECT
						
						Table newItem = new Table();
						newItem.End = true;
						newItem.Id = item.Id + 1;
						newItem.Name = "Ban " + newItem.Id;
						mData.add(newItem);
						notifyDataSetChanged();
						
						
					}
				});
		}
		else{
			
			tvName.setVisibility(View.VISIBLE);
			cb.setVisibility(View.VISIBLE);
			tvName.setText(item.Name);
			cb.setChecked(item.Checked);
			if(item.Available) layout.setBackgroundResource(R.drawable.shape_table_available) ;//.setBackgroundColor(Color.GREEN);
			else layout.setBackgroundResource(R.drawable.shape_table_busy) ;//layout.setBackgroundColor(Color.RED);
		}
		
		
		return convertView;
	}
}
